package pl.edu.pw.tm.zestawfiltrow.algorytmy;

import java.awt.Color;
import java.awt.image.BufferedImage;

public abstract class Filter {

	public abstract BufferedImage run(BufferedImage image); // metoda uruchamiajaca algorytm

	public abstract String getName(); // nazwa filtru	
	
	protected int colorOffset = 0;
	
	public int getRed(int rgb) {
		Color c = new Color(rgb);
		return c.getRed();
	}

	public int getBlue(int rgb) {
		Color c = new Color(rgb);
		return c.getBlue();
	}

	public int getGreen(int rgb) {
		Color c = new Color(rgb);
		return c.getGreen();
	}

	public int toRGB(int r, int g, int b) {
		r = truncate(r+colorOffset, 0, 255);
		g = truncate(g+colorOffset, 0, 255);
		b = truncate(b+colorOffset, 0, 255);
		Color c = new Color(r, g, b);
		return c.getRGB();
	}

	public int[][] getPixels(BufferedImage img) {
		if (img == null)
			return new int[0][0];
		int[][] pixelData = new int[img.getWidth()][img.getHeight()];

		for (int i = 0; i < img.getWidth(); i++) {
			for (int j = 0; j < img.getHeight(); j++) {
				pixelData[i][j] = img.getRGB(i, j);
			}
		}
		return pixelData;
	}
	private int truncate(int number, int min, int max){
		if(number < min) return min;
		else if(number > max) return max;
		return number;
	}
}
